package network

func FindConnect(guid int64) (ISocket, bool) {
	if v, ok := socketMap.Load(guid); ok {
		return v.(ISocket), true
	}
	return nil, false
}

func BroadcastData(data *SendPacket) {
	socketMap.Range(func(k, v interface{}) bool {
		c := v.(ISocket)
		p := GetSendPack(len(data.Data))
		copy(p.Data, data.Data)
		c.SendData(p)
		return true
	})
	PutSendPack(data)
}

func SendData(data *SendPacket) bool {
	if v, ok := socketMap.Load(data.Guid); ok {
		c := v.(ISocket)
		c.SendData(data)
		return true
	}
	return false
}

func CloseConnect(guid int64) {
	if v, ok := socketMap.Load(guid); ok {
		c := v.(ISocket)
		c.Close()
	}
}

func Receive() (*RecvPacketType, bool) {
	var p *RecvPacketType
	select {
	case p = <-readChan:
	default:
		return p, false
	}
	return p, true
}

func ReceiveBlock() *RecvPacketType {
	var p *RecvPacketType
	select {
	case p = <-readChan:
	}
	return p
}

func DeallocateRecvPacket(p *Packet) {
	PutRecvPack(p)
}

func DeallocateSendPacket(p *SendPacket) {
	PutSendPack(p)
}
